layui.use(['element', 'flow', 'jquery', 'api', 'form'], function () {
    var element = layui.element, flow = layui.flow, $ = layui.jquery,
        api = layui.api, form = layui.form;

    $('.child-nav-btn').on('click', function () {
        $(this).addClass('child-nav-btn-this').siblings().removeClass('child-nav-btn-this');
    });

    flow.load({
        elem: '#timeLineThought',
        done: function (page, next) {
            var lis = [];
            $.get('/assist/timeline?pageNum=' + page, function (res) {
                var resData = res.data;
                layui.each(resData.list, function (index, item) {
                    lis.push(formatTemplate(item, $('script[type="text/templateTimeLine"]').html()));
                });
                next(lis.join(''), page < resData.totalPage);
            });
        }
    });

    $('.addTimeLine').on('click', function () {
        $("#content").val('');
        if ($("#isLogin").val() === 'false') {
            layer.msg("登录后可添加", {shift: 6});
            return;
        }
        openTimeLineForm(function () {
            return getFormData("timeLineForm");
        });
    });
    // 修改时，采用jQuery事件委托机制
    $('#timeLineThought').on('click', 'i', function () {
        if ($("#isLogin").val() === 'false') {
            layer.msg("登录后可编辑", {shift: 6});
            return;
        }
        var id = $(this).attr('id'), content = $(this).parent('p').next('p').text();
        $("#content").val(content);
        openTimeLineForm(function () {
            var newData = getFormData("timeLineForm");
            newData['timeline.id'] = id;
            return newData;
        });
    });

    // 打开链接窗口,通过回调函数获取表单数据
    function openTimeLineForm(getData) {
        layer.open({
            title: false,
            type: 1,
            content: $("#timeLineDiv"),
            area: ['550px', '400px'],
            btn: ['提交', '取消'],
            btn1: function (index) {
                var data = getData();
                var content = data['timeline.content'];
                if (content === null || content === '') {
                    layer.msg('请输入内容', {shift: 6});
                    return;
                }
                api.saveTimeLine(data, function (res) {
                    if (res.code === 0) {
                        layer.close(index);
                        location.reload();
                    } else {
                        layer.msg(res.msg, {shift: 6});
                    }
                });
            }
        });
    }
});